Imports System.Text
Imports System.Web

Public Class SUM_Size
    Private Const Q As String = Chr(34)
    Private mlDatabaseID As Long
    Private msBaseURL As String = ""

    Private mbDataExists As Boolean = False

    Public Sub New( _
                ByVal sBaseURL As String, _
                ByVal lDatabaseID As Long)

        msBaseURL = sBaseURL
        mlDatabaseID = lDatabaseID
    End Sub

    Public ReadOnly Property DataExists() As Boolean
        Get
            Return mbDataExists
        End Get
    End Property

    Public Function Summerize() As String
        ' FIX: 2007-10-31 JCL Now includes attached chart graphic.

        Dim oQueueFactory As New SQLAutoDocLib.ScanQueue_Factory

        Dim oDatabase As New SQLAutoDocLib.Database(mlDatabaseID)
        oDatabase.Load()

        '---Get data
        Dim oFactory As New SQLAutoDocLib.TableSize_Factory
        Dim oData As DataTable = oFactory.AverageWeeklyDatabaseSize(12, mlDatabaseID)

        Dim oSum As New StringBuilder

        With oSum
            .Append("<span style=" & Q & "font-size: 12px; font-weight: bold;" & Q & ">")
            .Append("Database Size - Last 12 Weeks")
            .Append("<br/>")
            .Append("</span>")

            .Append("<span style=" & Q & "font-size: 10px; font-weight: normal;" & Q & ">")
            .Append("Last scan: " & oQueueFactory.GetLastQueuedRequest(mlDatabaseID, SQLAutoDocLib.ScanSchedule.Type.SizeScan))
            .Append("<br/>")
            .Append("</span>")

            If oData.Rows.Count > 0 Then
                mbDataExists = True

                .Append("<div style=" & Q & "vertical-align:top;" & Q & ">")

                '.Append("<img src=" & Q & MakeChartString_DatabaseSize(oData, "AvgSize") & Q & _
                '            " alt=" & Q & "12-week chart of database size for " & oDatabase.Name & Q & _
                '            " title=" & Q & "12-week chart of database size for " & oDatabase.Name & Q & _
                '            " style=" & Q & "border: solid 1px black; padding-right: 15px;" & Q & "/>")


                .Append("<a href=" & Q & "http://" & msBaseURL & "/SAD/SQSizeE.aspx?S=" & oDatabase.ServerID & _
                                                                                            "&D=" & oDatabase.ID & Q & _
                                                                                            " style=" & Q & "padding-right: 10px;" & Q & ">")

                Dim lSize As Long = 0
                If Not oData.Rows(oData.Rows.Count - 1).Item("AvgSize") Is System.DBNull.Value Then
                    lSize = oData.Rows(oData.Rows.Count - 1).Item("AvgSize")
                End If
                Dim sSize As String = ""
                If lSize = 0 Then
                    sSize = "0 kb"

                ElseIf lSize < 2500 Then
                    sSize = FormatNumber(lSize, 0, 0, 0, TriState.True) & " kb"

                ElseIf lSize < 1050000 Then
                    sSize = FormatNumber(lSize / 1024, 0, 0, 0, TriState.True) & " mb"

                Else
                    sSize = FormatNumber((lSize / 1024) / 1024, 0, 0, 0, TriState.True) & " gb"

                End If

                .Append("Average size this week: " & sSize)
                .Append("</a>")

                .Append("</div>")
            Else
                .Append("Empty database")
            End If
        End With

        Return oSum.ToString
    End Function

    Private Function MakeChartString_DatabaseSize(ByVal oData As DataTable, ByVal sFieldName As String) As String
        Dim oURL As New StringBuilder
        Dim iMaxValue As Integer = 0

        oURL.Append("http://" & msBaseURL & "/SAD/ilc.aspx?T=L&W=100&H=40&TH=2&FG=5959FF&BG=FFFFFF")

        For Each oRow As DataRow In oData.Rows
            If oRow.Item(sFieldName) Is System.DBNull.Value Then
                oURL.Append("&DP=0")
            Else
                oURL.Append("&DP=" & oRow.Item(sFieldName))

                If oRow.Item(sFieldName) > iMaxValue Then
                    iMaxValue = oRow.Item(sFieldName)
                End If
            End If
        Next

        If iMaxValue > 0 Then
            Return oURL.ToString
        Else
            Return ""
        End If
    End Function
End Class
